package xiaobange.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import xiaobange.common.R;
import xiaobange.entity.Employee;
import xiaobange.service.EmployeeService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService service;


    /**
     * 登录请求
     * @param HttpSession
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> log(HttpServletRequest HttpSession, @RequestBody Employee employee){
        //将密码进行MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()));
        //查询数据库
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee oneData = service.getOne(lambdaQueryWrapper);
        //判断用户名是否存在
        if (null == oneData){
            return R.error("用户名不存在!");
        }
        //判断用户输入的密码是否正确
        if (!oneData.getPassword().equals(employee.getPassword())){
            return R.error("密码错误!");
        }
        //判断用户是否被禁用
        if (oneData.getStatus() == 0){
            return R.error("已被禁用");
        }
        //将用户的ID存入session兵返回登录成功的结果
        javax.servlet.http.HttpSession session = HttpSession.getSession();
        session.setAttribute("employee",oneData.getId());
        return R.success(oneData);
    }

    /**
     * 退出登录
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest httpServletRequest){
        //获取session
        HttpSession session = httpServletRequest.getSession();
        //删除浏览器的session
        session.removeAttribute("employee");
        return R.success("退出成功");
    }


    /**
     * 添加管理员
     * @param httpServletRequest
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> add(HttpServletRequest httpServletRequest,@RequestBody Employee employee){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));//江密码进行MD5加密
        //employee.setCreateTime(LocalDateTime.now());//获取当前系统时间
        //employee.setUpdateTime(LocalDateTime.now());//获取当前系统时间
        //HttpSession session = httpServletRequest.getSession();//获取session
        //employee.setCreateUser((Long) session.getAttribute("employee"));//创建人ID
        //employee.setUpdateUser((Long) session.getAttribute("employee"));//最后修改人ID

        //调用service层执行添加操作
        service.save(employee);
        return R.success("添加成功!");
    }

    /**
     * 查询员工信息分页查询
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<IPage> Page(String name, int page, int pageSize){
        IPage employeeIPage = new Page<>(page,pageSize);//设置分页查询条件
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();//分页条件
        lambdaQueryWrapper.like(name != null,Employee::getUsername,name);//如果name有值的话就进行模糊查询
        lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);//设置排序条件
        service.page(employeeIPage,lambdaQueryWrapper);//查询
        return R.success(employeeIPage);
    }

    /**
     * 按ID查询
     * @param ID
     * @return
     */
    @GetMapping("/{ID}")
    public R<Employee> UpdateByID(@PathVariable Long ID){
        Employee oneDate = service.getById(ID);
        return R.success(oneDate);
    }


    /**
     * 修改员工信息
     * @param httpServletRequest
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> Update(HttpServletRequest httpServletRequest, @RequestBody Employee employee){
        //设置修改时间
//        employee.setUpdateTime(LocalDateTime.now());
        //获取修改人ID
//        employee.setUpdateUser((Long) httpServletRequest.getSession().getAttribute("employee"));
        boolean b = service.updateById(employee);
        if (b){
            return R.success("修改成功!");
        }else {
            return R.error("修改失败!");
        }
    }

}
